package com.zhongkeruan.order.controller;

import com.zhongkeruan.common.constants.StatusCode;
import com.zhongkeruan.common.model.Order;
import com.zhongkeruan.common.vo.Result;
import com.zhongkeruan.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yangyao
 * @Description: UserController
 * @date 2019/1/11 15:12
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    public Result getUserList() throws Exception{
        List<Order> orderList = orderService.getOrderList();
        return new Result(StatusCode.SUCCESS,"查询成功", orderList);
    }

    @GetMapping("/{id}")
    public Result getOrderById(@PathVariable("id") Long id) throws Exception{
        Order order = orderService.getOrderById(id);
        return new Result(StatusCode.SUCCESS,"查询成功", order);
    }

    /**
     * 根据用户id查询所有订单信息
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/{userId}/orders")
    public Result getOrderListByUserId(@PathVariable("userId") Long userId, HttpServletRequest request) throws Exception{
        String token = request.getHeader("access_token");
        List<Order> orderList = orderService.getOrderByUserId(userId);
       // Thread.sleep(8000);
        int a= 1/0;
        return new Result(StatusCode.SUCCESS,"查询成功", orderList);
    }

    /**
     * 新增订单信息
     * @return
     * @throws Exception
     */
    @PostMapping
    public Result addOrder(@RequestBody Order order) throws Exception{
        int result = orderService.addOrder(order);
        return new Result(StatusCode.SUCCESS,"插入成功");
    }
}
